package com.bo.sina.weibo.password;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import com.bo.WeiboConfig;



public class PwdEncodeJSRunner {
	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine engine = manager.getEngineByName("JavaScript");
	Invocable inv=null;
	public PwdEncodeJSRunner(){
		try {
			engine.eval(new FileReader(new File(WeiboConfig.getValue("rsafile"))));
		} catch (FileNotFoundException e) {
		} catch (ScriptException e) {
		}
		inv = (Invocable) engine;
	}
	public String executeFunction(String pubkey,String Servertime,String nonce,String pwd){
		String value=null;
		try {
			value=String.valueOf(inv.invokeFunction("RSAEncode",pubkey,"10001",pwd,Servertime,nonce));
		} catch (NoSuchMethodException e) {
		} catch (ScriptException e) {
		}
		return value;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String pubkey="EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443";
		PwdEncodeJSRunner njsrunner = new PwdEncodeJSRunner();
		String su = njsrunner.executeFunction(pubkey, "1371214895", "98ULWJ", "qwertyuiop[]\\");
		System.out.println(su);
	}
}
